# -*- coding: utf-8 -*-

import logging
import os
import re
import time
import datetime
import redis
import config

from scrapy.crawler import CrawlerProcess
from django.core.management.base import BaseCommand, CommandError
from scrapy.utils.log import configure_logging
from scrapy.utils.project import get_project_settings
from socar import settings
from sqlhelper import SqlHelper
from tools.decrypt_autohome_bbs import decrypt_autohome_bbs
from tools.parse_autohome_bbs import parse_autohome_bbs


# python manage.py decrypt_storage_bbs
class Command(BaseCommand):
    help = 'decrypt storage bbs'

    def add_arguments(self, parser):
        parser.add_argument('-a', action = 'append', dest = 'spargs', default = [],
                            help = 'set spider argument (may be repeated)')

    # 必须实现的方法
    def handle(self, *args, **options):
        os.chdir(settings.BASE_DIR)

        redis_db = redis.StrictRedis(**config.redis_config)
        start = 64398736
        end = 64700000

        count = end - start

        x = list(range(start, start + count))
        redis_db.sadd('bbs', *x)


def arglist_to_dict(arglist):
    """Convert a list of arguments like ['arg1=val1', 'arg2=val2', ...] to a
    dict
    """
    return dict(x.split('=', 1) for x in arglist)
